/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.command.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itgrail.grail.cqrs.annotation.CmdHandler;
import com.itgrail.grail.cqrs.command.CommandExecutorI;
import com.itgrail.grail.dto.PageResult;
import com.itgrail.grail.dto.ResultData;
import com.itgrail.grail.utils.reflect.BeanMapper;
import com.itgrail.pilot.app.command.cmo.query.PageDeptCmd;
import com.itgrail.pilot.app.command.co.DeptDetailCO;
import com.itgrail.pilot.domain.user.contant.OkrConstants;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.DepartmentMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.DepartmentDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@CmdHandler
public class PageDeptCmdExe implements CommandExecutorI<ResultData<PageResult<DeptDetailCO>>, PageDeptCmd> {

    @Autowired
    private DepartmentMapper mapper;

    @Override
    public ResultData execute(PageDeptCmd cmd) {
        ResultData resultData = ResultData.success(null);
        resultData.setData(page(cmd));
        return resultData;
    }


    protected PageResult<DeptDetailCO> page(PageDeptCmd cmd) {
        //查询部门列表
        Page<DepartmentDO> page = new Page<>(cmd.getPageNo(), cmd.getPageSize());
        LambdaQueryWrapper<DepartmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DepartmentDO::getIsDeleted, OkrConstants.NOT_DELETED);
        if (StringUtils.isNotEmpty(cmd.getName())) {
            queryWrapper.like(DepartmentDO::getName, cmd.getName());
        }
        if (StringUtils.isNotEmpty(cmd.getLeaderName())) {
            queryWrapper.like(DepartmentDO::getLeaderName, cmd.getLeaderName());
        }
        if (StringUtils.isNotEmpty(cmd.getLeaderId())) {
            queryWrapper.eq(DepartmentDO::getLeaderId, cmd.getLeaderId());
        }
        IPage<DepartmentDO> iPage = mapper.selectPage(page, queryWrapper);

        List<DeptDetailCO> deptDetailCOS = new ArrayList<>();
        if (null != iPage && !CollectionUtils.isEmpty(iPage.getRecords())) {
            deptDetailCOS = BeanMapper.mapList(iPage.getRecords(), DepartmentDO.class, DeptDetailCO.class);
        }
        PageResult<DeptDetailCO> result = new PageResult<>();
        deptDetailCOS.forEach(x -> {
            if (null != x.getParentId()) {
                LambdaQueryWrapper<DepartmentDO> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(DepartmentDO::getId, x.getParentId());
                DepartmentDO deptDO = mapper.selectOne(queryWrapper1);
                if (null != deptDO) {
                    x.setParentName(deptDO.getName());
                }
            }
        });
        result.setList(deptDetailCOS);
        result.setCurrentPage(iPage.getCurrent());
        result.setTotalCount(iPage.getTotal());
        result.setTotalPage(iPage.getPages());
        return result;
    }
}
